<?php
// PXFInstance::breakPoint($current_categories);

class ControllerAdmin extends PXFControllerAdmin
{
	public function __construct()
	{ 
		parent::__construct();
	}

	public function index()
	{
		$type = PXFRequest::reqGetOrDefault('type',false); 
		$this->tpl->assign('ITEM_TYPE',$type);
		$this->tpl->display('ajax_items.list.tpl');
	}
	
	public function items()
	{
		$sort = PXFRequest::reqGetOrDefault('sort','order'); 
		$type = PXFRequest::reqGetOrDefault('type',false); 
		$vsty = PXFRequest::reqGetOrDefault('vsty',false); 

		$params = array();
		$params['condition'] = new PXFCondition('1','1');
		if($type) $params['condition']->add(PXFCondition::opAND('typed',$type));
		if($vsty) $params['condition']->add(PXFCondition::opAND('visibility',$vsty));

	  $mod_items = PXFModel::getModel('item');
		$count = $mod_items->count($params);
		$paginator = new PXFPaginator($count,array());
		$params['order'] = array('item_'.$sort);
		$params['limit'] = array($paginator->from,$paginator->disp);
		$items = $mod_items->findBy($params);
		$result = array();
		foreach($items as $item)
		{
			$result[] = array('id' => $item->id, 'title' => $item->title, 'order' => $item->order);
		}
		echo pxf_json_encode(
				array(
					'items' => $result,
					'paginator' => array('range' => $paginator->range, 'current' => $paginator->page),
			));
	}

	public function items_save_order()
	{
		$mod_item = PXFModel::getModel('item');
		$items    = PXFRequest::reqGetOr404('items'); 
		foreach($items as $key => $value)
		{
			$item = $mod_item->find($key);
			$item->order = $value; 
			$item->save();
		}
		echo pxf_json_encode(array('msg' => 'Orden guardado correctamente'));
	}
}
?>
